package main.gui.dataobjecteditor;

import java.awt.Dimension;

import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import data.EditorObject;
import data.ObjectDatabase;

public class StringEditorPanel extends DataAttributePanel implements
		DocumentListener {

	private static final long serialVersionUID = 7845292973893875319L;

	private JTextField stringField;

	public StringEditorPanel(String attrName, String objectGUID,
			String fqClassName) {
		super(attrName, objectGUID, fqClassName);
	}

	@Override
	public void buildPanel(Object value) {
		stringField = new JTextField((String) value);
		stringField.setPreferredSize(new Dimension(MIN_WIDGET_WIDTH, MIN_WIDGET_HEIGHT));
		stringField.getDocument().addDocumentListener(this);
		this.add(stringField);
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		this.textChanged();
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		this.textChanged();
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		this.textChanged();
	}

	private void textChanged() {
		EditorObject editorObject = ObjectDatabase.getInstance()
				.getEditorObject(fqClassName, objectGUID);
		String value = stringField.getText();
		if (editorObject.validateValue(attrName, value)) {
			editorObject.getData().put(attrName, value);
		}
	}
}
